agora inbox for [email protected]  
help / color / mirror / Atom feed
bloated heapam.h
288+ messages / 4 participants
[nested] [flat]

* bloated heapam.h
@ 2008-05-09 18:56 Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-09 18:56 UTC (permalink / raw)
  To: pgsql-hackers

Hi,

I noticed heapam.h is included in way too many places.  This is bad IMHO
because heapam.h itself includes a lot of other headers.

A lot of these are easy to fix; the source files just need to include
some other headers.  Standard cleanup, I don't think anybody would
object to that.  For example,

Index: src/backend/access/gin/ginvacuum.c
===================================================================
RCS file: /home/alvherre/Code/cvs/pgsql/src/backend/access/gin/ginvacuum.c,v
retrieving revision 1.19
diff -c -p -r1.19 ginvacuum.c
*** src/backend/access/gin/ginvacuum.c  1 Jan 2008 19:45:46 -0000   1.19
--- src/backend/access/gin/ginvacuum.c  9 May 2008 18:44:31 -0000
***************
*** 15,24 ****
  #include "postgres.h"
  #include "access/genam.h"
  #include "access/gin.h"
- #include "access/heapam.h"
  #include "miscadmin.h"
  #include "storage/freespace.h"
! #include "storage/freespace.h"
  #include "commands/vacuum.h"
  
  typedef struct
--- 15,23 ----
  #include "postgres.h"
  #include "access/genam.h"
  #include "access/gin.h"
  #include "miscadmin.h"
  #include "storage/freespace.h"
! #include "storage/lmgr.h"
  #include "commands/vacuum.h"
  
  typedef struct


Others are more conflictive.  For example syncscan.c is keeping the
prototypes for its own functions on heapam.h.  Also pruneheap.c and
rewriteheap.c.  As a result, not only themselves need to include
heapam.h (without any other need for it), but they force some other
files into including heapam.h to get their prototypes.  I think this is
a mistake; I propose splitting those prototypes to their own files, and
#including those as appropriate.

Objections?

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-10 11:52 ` Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Zdenek Kotala @ 2008-05-10 11:52 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: pgsql-hackers

Alvaro Herrera napsal(a):

> Others are more conflictive.  For example syncscan.c is keeping the
> prototypes for its own functions on heapam.h.  Also pruneheap.c and
> rewriteheap.c.  As a result, not only themselves need to include
> heapam.h (without any other need for it), but they force some other
> files into including heapam.h to get their prototypes.  I think this is
> a mistake; I propose splitting those prototypes to their own files, and
> #including those as appropriate.
> 
> Objections?

I have similar thing in my TODO list. See my patch from March commit fest and 
discussion. I need solve two main issues - remove postgres.h from binaries and 
keep history of structures (for pg_upgrade project).

My idea is split structures and functions in separate header files.

		Zdenek

http://archives.postgresql.org/pgsql-patches/2007-10/msg00197.php
http://archives.postgresql.org/pgsql-patches/2008-04/msg00149.php


		



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
@ 2008-05-10 19:26   ` Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-10 19:26 UTC (permalink / raw)
  To: Zdenek Kotala <[email protected]>; +Cc: pgsql-hackers

Zdenek Kotala wrote:
> Alvaro Herrera napsal(a):
>
>> Others are more conflictive.  For example syncscan.c is keeping the
>> prototypes for its own functions on heapam.h.  Also pruneheap.c and
>> rewriteheap.c.  As a result, not only themselves need to include
>> heapam.h (without any other need for it), but they force some other
>> files into including heapam.h to get their prototypes.  I think this is
>> a mistake; I propose splitting those prototypes to their own files, and
>> #including those as appropriate.
>>
>> Objections?
>
> I have similar thing in my TODO list. See my patch from March commit fest 
> and discussion. I need solve two main issues - remove postgres.h from 
> binaries and keep history of structures (for pg_upgrade project).

Yeah, I remember that.  Is there any progress on that front?

BTW I noticed that I was a bit careless in the description.  rewriteheap
already has its own rewriteheap.h file; and there's no point at all in
separating pruneheap.c declarations into another file.

The one that makes a bit more sense is a new syncscan.h.  And there are
a lot of things in heapam.h that actually correspond to tuple
manipulation (heap_form_tuple and so on), so perhaps a new header file
would be appropriate, but there's already htup.h which contains
tuple-related stuff.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-10 21:10     ` Tom Lane <[email protected]>
  2008-05-11 04:30       ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  0 siblings, 2 replies; 288+ messages in thread

From: Tom Lane @ 2008-05-10 21:10 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera <[email protected]> writes:
> The one that makes a bit more sense is a new syncscan.h.  And there are
> a lot of things in heapam.h that actually correspond to tuple
> manipulation (heap_form_tuple and so on), so perhaps a new header file
> would be appropriate, but there's already htup.h which contains
> tuple-related stuff.

After actually looking at the header a bit ...

+1 for moving fastgetattr, heap_getattr, and the heaptuple.c functions
to htup.h.  I don't see any big gain from relocating the other stuff;
it seems to largely all use about the same set of typedefs.

It looks to me actually that a large part of your complaint is that
heapam.h #includes more than it has to.  Have you tried just cutting its
#include list to the minimum needed to compile its function declarations?
Likely this would force more #includes in .c files but I don't object
to that.  (I might be wrong, but I believe that Bruce's script for
removing "unnecessary" #includes is not bright enough to make such
tradeoffs, so it'd let bloated #include lists in headers survive.)

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
@ 2008-05-11 04:30       ` Alvaro Herrera <[email protected]>
  2008-05-11 04:49         ` Re: bloated heapam.h Tom Lane <[email protected]>
  1 sibling, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-11 04:30 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Tom Lane wrote:

> +1 for moving fastgetattr, heap_getattr, and the heaptuple.c functions
> to htup.h.  I don't see any big gain from relocating the other stuff;
> it seems to largely all use about the same set of typedefs.

Ultimately that was my conclusion too.  I tried moving the heaptuple.c
functions to htup.h, but hit the problem that pg_dump.c wants to include
that file.  It then fails to compile because it doesn't know Datum
(defined in postgres.h, so unavailable to frontend programs).  I worked
around that by enclosing the prototypes in #ifndef FRONTEND, but that
seems ugly.

Apparently the reason for pg_dump.c to need htup.h is getAttrName which
needs system columns' attribute numbers.  Of course, the first thing
that comes to mind is that we should fix pg_dump to not require that
header in the first place.  Perhaps we can get the names by querying the
server, at the same time we get the user column names.  (Apparently this
is only used to dump unique indexes on system columns.)

The easiest actual solution, however, seems to be to move those
attribute numbers to a separate header, say access/sysattrs.h.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 04:30       ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-11 04:49         ` Tom Lane <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Tom Lane @ 2008-05-11 04:49 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera <[email protected]> writes:
> Apparently the reason for pg_dump.c to need htup.h is getAttrName which
> needs system columns' attribute numbers.  Of course, the first thing
> that comes to mind is that we should fix pg_dump to not require that
> header in the first place.  Perhaps we can get the names by querying the
> server, at the same time we get the user column names.  (Apparently this
> is only used to dump unique indexes on system columns.)

> The easiest actual solution, however, seems to be to move those
> attribute numbers to a separate header, say access/sysattrs.h.

Yeah, probably.  There is certainly no reason for a frontend program
to be dealing in Datum; but it isn't unreasonable for it to know
the system column numbers, since it can see those in the catalogs.

I'm sure we have work in front of us to get these things separated
out a bit better.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
@ 2008-05-11 18:43       ` Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  1 sibling, 1 reply; 288+ messages in thread

From: Tom Lane @ 2008-05-11 18:43 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera <[email protected]> writes:
> So here's a patch (includes the #ifndef FRONTEND hack in htup.h.)

I like this except for the #ifndef FRONTEND hack --- you're going to
need to fix that before applying.  I'm good with doing that by pushing
the system attribute numbers into a separate header.

BTW, you didn't compress the patch, which likely explains why it didn't
show up on -hackers.  If you want to post a shorter form, I think that
the diffs in the header files are the only interesting part; the ensuing
additions in .c files are just mechanical.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
@ 2008-05-12 00:06         ` Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 00:06 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Tom Lane wrote:
> Alvaro Herrera <[email protected]> writes:
> > So here's a patch (includes the #ifndef FRONTEND hack in htup.h.)
> 
> I like this except for the #ifndef FRONTEND hack --- you're going to
> need to fix that before applying.  I'm good with doing that by pushing
> the system attribute numbers into a separate header.

Committed with the attribute numbers moved to access/sysattr.h, which is
what pg_dump now uses.

Thanks.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 00:21           ` Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:30             ` Re: bloated heapam.h Tom Lane <[email protected]>
  0 siblings, 2 replies; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 00:21 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera wrote:
> Tom Lane wrote:
> > Alvaro Herrera <[email protected]> writes:
> > > So here's a patch (includes the #ifndef FRONTEND hack in htup.h.)
> > 
> > I like this except for the #ifndef FRONTEND hack --- you're going to
> > need to fix that before applying.  I'm good with doing that by pushing
> > the system attribute numbers into a separate header.
> 
> Committed with the attribute numbers moved to access/sysattr.h, which is
> what pg_dump now uses.

Oops :-(  I just noticed that I removed bufmgr.h from bufpage.h, which
is a change you had objected to previously :-(

http://archives.postgresql.org/pgsql-patches/2008-04/msg00149.php

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 00:28             ` Alvaro Herrera <[email protected]>
  2008-05-12 00:35               ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  1 sibling, 2 replies; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 00:28 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera wrote:

> Oops :-(  I just noticed that I removed bufmgr.h from bufpage.h, which
> is a change you had objected to previously :-(

However, it seems the right fix is to move BufferGetPageSize and
BufferGetPage from bufpage.h to bufmgr.h.

(Digging further, it seems like bufpage.h should also include transam.h
in order to get TransactionIdIsNormal ... I start to wonder how many
problems of this nature we have on our headers.  Without having a way to
detect whether the defined macros are valid, it seems hard to check
programatically, however.)

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 00:35               ` Tom Lane <[email protected]>
  2008-05-12 01:04                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  1 sibling, 1 reply; 288+ messages in thread

From: Tom Lane @ 2008-05-12 00:35 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera <[email protected]> writes:
> However, it seems the right fix is to move BufferGetPageSize and
> BufferGetPage from bufpage.h to bufmgr.h.

That sounds sane if it fixes the problem.

> (Digging further, it seems like bufpage.h should also include transam.h
> in order to get TransactionIdIsNormal ... I start to wonder how many
> problems of this nature we have on our headers.  Without having a way to
> detect whether the defined macros are valid, it seems hard to check
> programatically, however.)

Yeah, I'm certain there's some other problems of the same kind, but I
don't see any easy way to find 'em.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:35               ` Re: bloated heapam.h Tom Lane <[email protected]>
@ 2008-05-12 01:04                 ` Alvaro Herrera <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 01:04 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Tom Lane wrote:
> Alvaro Herrera <[email protected]> writes:
> > However, it seems the right fix is to move BufferGetPageSize and
> > BufferGetPage from bufpage.h to bufmgr.h.
> 
> That sounds sane if it fixes the problem.

Actually it's not, because bufmgr.h does not include bufpage.h, and it
knows nothing about "pages".

However, I think most additions of bufmgr.h to source files are not all
that bogus -- a lot of the warnings I got were about
BufferAccessStrategyType.  I'm still checking what's the best way to
deal with this, and if I can't find anything else I'll re-add bufmgr.h
to bufpage.h.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 12:22               ` Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  1 sibling, 1 reply; 288+ messages in thread

From: Zdenek Kotala @ 2008-05-12 12:22 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Alvaro Herrera wrote:
> Alvaro Herrera wrote:
> 
>> Oops :-(  I just noticed that I removed bufmgr.h from bufpage.h, which
>> is a change you had objected to previously :-(
> 
> However, it seems the right fix is to move BufferGetPageSize and
> BufferGetPage from bufpage.h to bufmgr.h.

+1 It makes more sense.

> (Digging further, it seems like bufpage.h should also include transam.h
> in order to get TransactionIdIsNormal ... I start to wonder how many
> problems of this nature we have on our headers.  Without having a way to
> detect whether the defined macros are valid, it seems hard to check
> programatically, however.)
> 

I attached script which should check it. In first step it runs C preprocessor on 
each header (postgres.h is included as well). The output from first step is 
processed again with preprocessor and define.h is included. Define.h contains 
"all" used macros in following format:

#define SIGABRT "NOT_EXPANDED_SIGABRT"

Main problem is how to generate define.h. I used following magic:

grep "^#define" `find . -name "*.h"` | cut -d" "  -f 2 | cut -f 1 | cut -f 1 -d"("

but it generates some noise as well. Maybe some Perl or AWK magic should be better.

		Zdenek


Attachments:

  [application/x-shellscript] test.sh (279B, ../../[email protected]/2-test.sh)
  download

^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
@ 2008-05-12 16:13                 ` Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 16:13 UTC (permalink / raw)
  To: Zdenek Kotala <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Zdenek Kotala wrote:
> Alvaro Herrera wrote:

>> (Digging further, it seems like bufpage.h should also include transam.h
>> in order to get TransactionIdIsNormal ... I start to wonder how many
>> problems of this nature we have on our headers.  Without having a way to
>> detect whether the defined macros are valid, it seems hard to check
>> programatically, however.)
>
> I attached script which should check it. In first step it runs C 
> preprocessor on each header (postgres.h is included as well). The output 
> from first step is processed again with preprocessor and define.h is 
> included. Define.h contains "all" used macros in following format:
>
> #define SIGABRT "NOT_EXPANDED_SIGABRT"
>
> Main problem is how to generate define.h. I used following magic:
>
> grep "^#define" `find . -name "*.h"` | cut -d" "  -f 2 | cut -f 1 | cut -f 1 -d"("
>
> but it generates some noise as well. Maybe some Perl or AWK magic should be better.

So were you able to detect anything bogus with this technique?

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 19:08                   ` Zdenek Kotala <[email protected]>
  2008-05-12 19:26                     ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Zdenek Kotala @ 2008-05-12 19:08 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Alvaro Herrera napsal(a):
> Zdenek Kotala wrote:
>> Alvaro Herrera wrote:
> 
>>> (Digging further, it seems like bufpage.h should also include transam.h
>>> in order to get TransactionIdIsNormal ... I start to wonder how many
>>> problems of this nature we have on our headers.  Without having a way to
>>> detect whether the defined macros are valid, it seems hard to check
>>> programatically, however.)
>> I attached script which should check it. In first step it runs C 
>> preprocessor on each header (postgres.h is included as well). The output 
>> from first step is processed again with preprocessor and define.h is 
>> included. Define.h contains "all" used macros in following format:
>>
>> #define SIGABRT "NOT_EXPANDED_SIGABRT"
>>
>> Main problem is how to generate define.h. I used following magic:
>>
>> grep "^#define" `find . -name "*.h"` | cut -d" "  -f 2 | cut -f 1 | cut -f 1 -d"("
>>
>> but it generates some noise as well. Maybe some Perl or AWK magic should be better.
> 
> So were you able to detect anything bogus with this technique?
> 

No, everything looks OK.

		Zdenek



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
@ 2008-05-12 19:26                     ` Alvaro Herrera <[email protected]>
  2008-05-12 19:52                       ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 19:26 UTC (permalink / raw)
  To: Zdenek Kotala <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Zdenek Kotala wrote:
> Alvaro Herrera napsal(a):
>> Zdenek Kotala wrote:

>>> I attached script which should check it. In first step it runs C  
>>> preprocessor on each header (postgres.h is included as well). The 
>>> output from first step is processed again with preprocessor and 
>>> define.h is included.

>> So were you able to detect anything bogus with this technique?
>
> No, everything looks OK.

Well, then it is not working, because transam.h is missing from
bufpage.h ...

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 19:26                     ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 19:52                       ` Zdenek Kotala <[email protected]>
  2008-05-12 20:02                         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Zdenek Kotala @ 2008-05-12 19:52 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Alvaro Herrera napsal(a):
> Zdenek Kotala wrote:
>> Alvaro Herrera napsal(a):
>>> Zdenek Kotala wrote:
> 
>>>> I attached script which should check it. In first step it runs C  
>>>> preprocessor on each header (postgres.h is included as well). The 
>>>> output from first step is processed again with preprocessor and 
>>>> define.h is included.
> 
>>> So were you able to detect anything bogus with this technique?
>> No, everything looks OK.
> 
> Well, then it is not working, because transam.h is missing from
> bufpage.h ...
> 

It tried catch problems with defines not with datatypes. If you mean that 
TransactionID is not defined.

I try to play with lint now if it gets better results.

		Zdenek



	



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 19:26                     ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:52                       ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
@ 2008-05-12 20:02                         ` Alvaro Herrera <[email protected]>
  2008-05-12 21:33                           ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 20:02 UTC (permalink / raw)
  To: Zdenek Kotala <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Zdenek Kotala wrote:
> Alvaro Herrera napsal(a):

>> Well, then it is not working, because transam.h is missing from
>> bufpage.h ...
>
> It tried catch problems with defines not with datatypes. If you mean that 
> TransactionID is not defined.

No, I mean that TransactionIdIsNormal() is used in PageSetPrunable().

> I try to play with lint now if it gets better results.

Ok, good.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 19:26                     ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:52                       ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 20:02                         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 21:33                           ` Zdenek Kotala <[email protected]>
  2008-05-13 15:42                             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Zdenek Kotala @ 2008-05-12 21:33 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Alvaro Herrera napsal(a):

>> I try to play with lint now if it gets better results.
> 
> Ok, good.

Hmm, It generates a lot of unnecessary includes in *.c files. I have checked 
only couple of them and it seems that they are really unnecessary. A attach 
output. Unfortunately, it does not detect missing heapam.h from bufpage.h. 
However, I have not tested all magic switches yet :-).  There are also several 
reports about system headers file, but it could be platform specific warning.

	Zdenek


Attachments:

  [application/x-gzip] include.out.gz (4.5K, ../../[email protected]/2-include.out.gz)
  download

^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 19:26                     ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:52                       ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 20:02                         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 21:33                           ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
@ 2008-05-13 15:42                             ` Alvaro Herrera <[email protected]>
  2008-05-15 15:48                               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  0 siblings, 1 reply; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-13 15:42 UTC (permalink / raw)
  To: Zdenek Kotala <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Zdenek Kotala wrote:
> Alvaro Herrera napsal(a):
>
>>> I try to play with lint now if it gets better results.
>>
>> Ok, good.
>
> Hmm, It generates a lot of unnecessary includes in *.c files. I have 
> checked only couple of them and it seems that they are really 
> unnecessary. A attach output. Unfortunately, it does not detect missing 
> heapam.h from bufpage.h. However, I have not tested all magic switches 
> yet :-).  There are also several reports about system headers file, but 
> it could be platform specific warning.

Interesting.  It seems that Bruce's script could be replaced by lint.
Please share the switches you used.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:28             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 12:22               ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 16:13                 ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:08                   ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 19:26                     ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 19:52                       ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-12 20:02                         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 21:33                           ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-13 15:42                             ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-15 15:48                               ` Zdenek Kotala <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Zdenek Kotala @ 2008-05-15 15:48 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-hackers

Alvaro Herrera wrote:
> Zdenek Kotala wrote:
>> Alvaro Herrera napsal(a):
>>
>>>> I try to play with lint now if it gets better results.
>>> Ok, good.
>> Hmm, It generates a lot of unnecessary includes in *.c files. I have 
>> checked only couple of them and it seems that they are really 
>> unnecessary. A attach output. Unfortunately, it does not detect missing 
>> heapam.h from bufpage.h. However, I have not tested all magic switches 
>> yet :-).  There are also several reports about system headers file, but 
>> it could be platform specific warning.
> 
> Interesting.  It seems that Bruce's script could be replaced by lint.
> Please share the switches you used.
> 

I used following switches which only shows unused included file. I run command 
in backend directory.

lint -I ../include -errtags -errhdr=%all -Ncheck=%none -Nlevel=1 -erroff=%all 
-erroff=no%E_INCL_NUSD -c  `find . -name "*.c"` > include.out

Good to mention that I use lint from Sun Studio 12.


		Zdenek



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
@ 2008-05-12 00:30             ` Tom Lane <[email protected]>
  2008-05-12 16:10               ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  1 sibling, 1 reply; 288+ messages in thread

From: Tom Lane @ 2008-05-12 00:30 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Alvaro Herrera <[email protected]> writes:
> Oops :-(  I just noticed that I removed bufmgr.h from bufpage.h, which
> is a change you had objected to previously :-(
> http://archives.postgresql.org/pgsql-patches/2008-04/msg00149.php

Hmm.  I did notice that the patch seemed to need to add bufmgr.h
inclusions to an awful lot of files, but I'd forgotten the argument
about the bufpage.h macros needing it.  Do you want to undo that
aspect of it?

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* Re: bloated heapam.h
  2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 11:52 ` Re: bloated heapam.h Zdenek Kotala <[email protected]>
  2008-05-10 19:26   ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-10 21:10     ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-11 18:43       ` Re: bloated heapam.h Tom Lane <[email protected]>
  2008-05-12 00:06         ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:21           ` Re: bloated heapam.h Alvaro Herrera <[email protected]>
  2008-05-12 00:30             ` Re: bloated heapam.h Tom Lane <[email protected]>
@ 2008-05-12 16:10               ` Alvaro Herrera <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Alvaro Herrera @ 2008-05-12 16:10 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Zdenek Kotala <[email protected]>; pgsql-hackers

Tom Lane wrote:
> Alvaro Herrera <[email protected]> writes:
> > Oops :-(  I just noticed that I removed bufmgr.h from bufpage.h, which
> > is a change you had objected to previously :-(
> > http://archives.postgresql.org/pgsql-patches/2008-04/msg00149.php
> 
> Hmm.  I did notice that the patch seemed to need to add bufmgr.h
> inclusions to an awful lot of files, but I'd forgotten the argument
> about the bufpage.h macros needing it.  Do you want to undo that
> aspect of it?

Done -- I put back bufmgr.h into bufpage.h.  Surely there is a better
structure for this, perhaps involving more header files, but I don't
have time for that ATM.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.



^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09 Andres Freund <[email protected]>
  0 siblings, 0 replies; 288+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 288+ messages in thread


end of thread, other threads:[~2023-01-21 00:09 UTC | newest]

Thread overview: 288+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-05-09 18:56 bloated heapam.h Alvaro Herrera <[email protected]>
2008-05-10 11:52 ` Zdenek Kotala <[email protected]>
2008-05-10 19:26   ` Alvaro Herrera <[email protected]>
2008-05-10 21:10     ` Tom Lane <[email protected]>
2008-05-11 04:30       ` Alvaro Herrera <[email protected]>
2008-05-11 04:49         ` Tom Lane <[email protected]>
2008-05-11 18:43       ` Tom Lane <[email protected]>
2008-05-12 00:06         ` Alvaro Herrera <[email protected]>
2008-05-12 00:21           ` Alvaro Herrera <[email protected]>
2008-05-12 00:28             ` Alvaro Herrera <[email protected]>
2008-05-12 00:35               ` Tom Lane <[email protected]>
2008-05-12 01:04                 ` Alvaro Herrera <[email protected]>
2008-05-12 12:22               ` Zdenek Kotala <[email protected]>
2008-05-12 16:13                 ` Alvaro Herrera <[email protected]>
2008-05-12 19:08                   ` Zdenek Kotala <[email protected]>
2008-05-12 19:26                     ` Alvaro Herrera <[email protected]>
2008-05-12 19:52                       ` Zdenek Kotala <[email protected]>
2008-05-12 20:02                         ` Alvaro Herrera <[email protected]>
2008-05-12 21:33                           ` Zdenek Kotala <[email protected]>
2008-05-13 15:42                             ` Alvaro Herrera <[email protected]>
2008-05-15 15:48                               ` Zdenek Kotala <[email protected]>
2008-05-12 00:30             ` Tom Lane <[email protected]>
2008-05-12 16:10               ` Alvaro Herrera <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-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