agora inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH] Add option to modify sync commit per subscription
270+ messages / 2 participants
[nested] [flat]

* [PATCH] Add option to modify sync commit per subscription
@ 2017-03-06 12:07  Petr Jelinek <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Petr Jelinek @ 2017-03-06 12:07 UTC (permalink / raw)

This also changes default behaviour of subscription workers to
synchronous_commit = off
---
 doc/src/sgml/catalogs.sgml                 | 11 +++++++
 doc/src/sgml/ref/alter_subscription.sgml   |  2 ++
 doc/src/sgml/ref/create_subscription.sgml  | 12 ++++++++
 src/backend/catalog/pg_subscription.c      |  1 +
 src/backend/commands/subscriptioncmds.c    | 49 ++++++++++++++++++++++++------
 src/backend/replication/logical/launcher.c |  2 +-
 src/backend/replication/logical/worker.c   | 10 ++++++
 src/bin/pg_dump/pg_dump.c                  | 12 +++++++-
 src/bin/pg_dump/pg_dump.h                  |  1 +
 src/bin/pg_dump/t/002_pg_dump.pl           |  2 +-
 src/bin/psql/describe.c                    |  5 ++-
 src/include/catalog/pg_subscription.h      | 11 ++++---
 src/test/regress/expected/subscription.out | 27 ++++++++--------
 src/test/regress/sql/subscription.sql      |  3 +-
 14 files changed, 116 insertions(+), 32 deletions(-)

diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index c531c73..9e072ea 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -6414,6 +6414,17 @@
      </row>
 
      <row>
+      <entry><structfield>subsynccommit</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>
+       If true, the apply for the subscription will run with
+       <literal>synchronous_commit</literal> set to <literal>local</literal>.
+       Otherwise it will have it set to <literal>false</literal>.
+      </entry>
+     </row>
+
+     <row>
       <entry><structfield>subconninfo</structfield></entry>
       <entry><type>text</type></entry>
       <entry></entry>
diff --git a/doc/src/sgml/ref/alter_subscription.sgml b/doc/src/sgml/ref/alter_subscription.sgml
index 6f94247..572e370 100644
--- a/doc/src/sgml/ref/alter_subscription.sgml
+++ b/doc/src/sgml/ref/alter_subscription.sgml
@@ -26,6 +26,8 @@ ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> WITH ( <rep
 <phrase>where <replaceable class="PARAMETER">suboption</replaceable> can be:</phrase>
 
     SLOT NAME = <replaceable class="PARAMETER">slot_name</replaceable>
+    SLOT NAME = slot_name
+    | SYNCHRONOUS_COMMIT = boolean
 
 ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> SET PUBLICATION <replaceable class="PARAMETER">publication_name</replaceable> [, ...] { REFRESH WITH ( <replaceable class="PARAMETER">puboption</replaceable> [, ... ] ) | NOREFRESH }
 ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> REFRESH PUBLICATION WITH ( <replaceable class="PARAMETER">puboption</replaceable> [, ... ] )
diff --git a/doc/src/sgml/ref/create_subscription.sgml b/doc/src/sgml/ref/create_subscription.sgml
index 8f3c30b..479f4e4 100644
--- a/doc/src/sgml/ref/create_subscription.sgml
+++ b/doc/src/sgml/ref/create_subscription.sgml
@@ -32,6 +32,7 @@ CREATE SUBSCRIPTION <replaceable class="PARAMETER">subscription_name</replaceabl
     | CREATE SLOT | NOCREATE SLOT
     | SLOT NAME = <replaceable class="PARAMETER">slot_name</replaceable>
     | COPY DATA | NOCOPY DATA
+    | SYNCHRONOUS_COMMIT = boolean
     | NOCONNECT
 </synopsis>
  </refsynopsisdiv>
@@ -148,6 +149,17 @@ CREATE SUBSCRIPTION <replaceable class="PARAMETER">subscription_name</replaceabl
    </varlistentry>
 
    <varlistentry>
+    <term><literal>SYNCHRONOUS_COMMIT = <replaceable class="parameter">boolean</replaceable></literal></term>
+    <listitem>
+     <para>
+      The value of this parameter overrides the
+      <xref linkend="guc-synchronous-commit">synchronous_commit setting.
+      The default value is <literal>false<literal>.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
     <term>NOCONNECT</term>
     <listitem>
      <para>
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index e420ec1..e7a1634 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -68,6 +68,7 @@ GetSubscription(Oid subid, bool missing_ok)
 	sub->name = pstrdup(NameStr(subform->subname));
 	sub->owner = subform->subowner;
 	sub->enabled = subform->subenabled;
+	sub->synccommit = subform->subsynccommit;
 
 	/* Get conninfo */
 	datum = SysCacheGetAttr(SUBSCRIPTIONOID,
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 0784ca7..8f201b2 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -60,12 +60,13 @@ static List *fetch_table_list(WalReceiverConn *wrconn, List *publications);
 static void
 parse_subscription_options(List *options, bool *connect, bool *enabled_given,
 						   bool *enabled, bool *create_slot, char **slot_name,
-						   bool *copy_data)
+						   bool *copy_data, bool *synchronous_commit)
 {
 	ListCell   *lc;
 	bool		connect_given = false;
 	bool		create_slot_given = false;
 	bool		copy_data_given = false;
+	bool		synchronous_commit_given = false;
 
 	if (connect)
 		*connect = true;
@@ -80,6 +81,8 @@ parse_subscription_options(List *options, bool *connect, bool *enabled_given,
 		*slot_name = NULL;
 	if (copy_data)
 		*copy_data = true;
+	if (synchronous_commit)
+		*synchronous_commit = false;
 
 	/* Parse options */
 	foreach (lc, options)
@@ -165,6 +168,16 @@ parse_subscription_options(List *options, bool *connect, bool *enabled_given,
 			copy_data_given = true;
 			*copy_data = !defGetBoolean(defel);
 		}
+		else if (strcmp(defel->defname, "synchronous_commit") == 0 && synchronous_commit)
+		{
+			if (synchronous_commit_given)
+				ereport(ERROR,
+						(errcode(ERRCODE_SYNTAX_ERROR),
+						 errmsg("conflicting or redundant options")));
+
+			synchronous_commit_given = true;
+			*synchronous_commit = defGetBoolean(defel);
+		}
 		else
 			elog(ERROR, "unrecognized option: %s", defel->defname);
 	}
@@ -269,6 +282,7 @@ CreateSubscription(CreateSubscriptionStmt *stmt, bool isTopLevel)
 	bool		enabled_given;
 	bool		enabled;
 	bool		copy_data;
+	bool		synchronous_commit;
 	char	   *conninfo;
 	char	   *slotname;
 	char		originname[NAMEDATALEN];
@@ -280,7 +294,8 @@ CreateSubscription(CreateSubscriptionStmt *stmt, bool isTopLevel)
 	 * Connection and publication should not be specified here.
 	 */
 	parse_subscription_options(stmt->options, &connect, &enabled_given,
-							   &enabled, &create_slot, &slotname, &copy_data);
+							   &enabled, &create_slot, &slotname, &copy_data,
+							   &synchronous_commit);
 
 	/*
 	 * Since creating a replication slot is not transactional, rolling back
@@ -330,6 +345,8 @@ CreateSubscription(CreateSubscriptionStmt *stmt, bool isTopLevel)
 		DirectFunctionCall1(namein, CStringGetDatum(stmt->subname));
 	values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
 	values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
+	values[Anum_pg_subscription_subsynccommit - 1] =
+		BoolGetDatum(synchronous_commit);
 	values[Anum_pg_subscription_subconninfo - 1] =
 		CStringGetTextDatum(conninfo);
 	values[Anum_pg_subscription_subslotname - 1] =
@@ -581,14 +598,26 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
 	{
 		case ALTER_SUBSCRIPTION_OPTIONS:
 			{
-				char *slot_name;
+				char   *slot_name;
+				bool	synchronous_commit;
+				Form_pg_subscription form;
+
+				form = (Form_pg_subscription) GETSTRUCT(tup);
+				synchronous_commit = form->subsynccommit;
 
 				parse_subscription_options(stmt->options, NULL, NULL, NULL,
-										   NULL, &slot_name, NULL);
+										   NULL, &slot_name, NULL,
+										   &synchronous_commit);
 
-				values[Anum_pg_subscription_subslotname - 1] =
-					DirectFunctionCall1(namein, CStringGetDatum(slot_name));
-				replaces[Anum_pg_subscription_subslotname - 1] = true;
+				if (slot_name)
+				{
+					values[Anum_pg_subscription_subslotname - 1] =
+						DirectFunctionCall1(namein, CStringGetDatum(slot_name));
+					replaces[Anum_pg_subscription_subslotname - 1] = true;
+				}
+				values[Anum_pg_subscription_subsynccommit - 1] =
+					BoolGetDatum(synchronous_commit);
+				replaces[Anum_pg_subscription_subsynccommit - 1] = true;
 
 				update_tuple = true;
 				break;
@@ -601,7 +630,7 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
 
 				parse_subscription_options(stmt->options, NULL,
 										   &enabled_given, &enabled, NULL,
-										   NULL, NULL);
+										   NULL, NULL, NULL);
 				Assert(enabled_given);
 
 				values[Anum_pg_subscription_subenabled - 1] =
@@ -626,7 +655,7 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
 				Subscription   *sub = GetSubscription(subid, false);
 
 				parse_subscription_options(stmt->options, NULL, NULL, NULL,
-										   NULL, NULL, &copy_data);
+										   NULL, NULL, &copy_data, NULL);
 
 				values[Anum_pg_subscription_subpublications - 1] =
 					 publicationListToArray(stmt->publication);
@@ -652,7 +681,7 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
 				Subscription   *sub = GetSubscription(subid, false);
 
 				parse_subscription_options(stmt->options, NULL, NULL, NULL,
-										   NULL, NULL, &copy_data);
+										   NULL, NULL, &copy_data, NULL);
 
 				AlterSubscription_refresh(sub, copy_data);
 
diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 255b225..0d3ec27 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -126,7 +126,7 @@ get_subscription_list(void)
 		 */
 		oldcxt = MemoryContextSwitchTo(resultcxt);
 
-		sub = (Subscription *) palloc(sizeof(Subscription));
+		sub = (Subscription *) palloc0(sizeof(Subscription));
 		sub->oid = HeapTupleGetOid(tup);
 		sub->dbid = subform->subdbid;
 		sub->owner = subform->subowner;
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index fc01cd3..ffab8a4 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -1410,6 +1410,11 @@ reread_subscription(void)
 
 	MemoryContextSwitchTo(oldctx);
 
+	/* Change synchronous commit according to the user's wishes */
+	SetConfigOption("synchronous_commit",
+					MySubscription->synccommit ? "local" : "off",
+					PGC_BACKEND, PGC_S_OVERRIDE);
+
 	if (started_tx)
 		CommitTransactionCommand();
 
@@ -1478,6 +1483,11 @@ ApplyWorkerMain(Datum main_arg)
 	MySubscriptionValid = true;
 	MemoryContextSwitchTo(oldctx);
 
+	/* Setup synchronous commit according to the user's wishes */
+	SetConfigOption("synchronous_commit",
+					MySubscription->synccommit ? "local" : "off",
+					PGC_BACKEND, PGC_S_OVERRIDE);
+
 	if (!MySubscription->enabled)
 	{
 		ereport(LOG,
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index b3d95d7..14cfa69 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -3653,6 +3653,7 @@ getSubscriptions(Archive *fout)
 	int			i_subname;
 	int			i_rolname;
 	int			i_subenabled;
+	int			i_subsynccommit;
 	int			i_subconninfo;
 	int			i_subslotname;
 	int			i_subpublications;
@@ -3670,7 +3671,8 @@ getSubscriptions(Archive *fout)
 	appendPQExpBuffer(query,
 					  "SELECT s.tableoid, s.oid, s.subname,"
 					  "(%s s.subowner) AS rolname, s.subenabled, "
-					  " s.subconninfo, s.subslotname, s.subpublications "
+					  " s.subsynccommit, s.subconninfo, s.subslotname, "
+					  " s.subpublications "
 					  "FROM pg_catalog.pg_subscription s "
 					  "WHERE s.subdbid = (SELECT oid FROM pg_catalog.pg_database"
 					  "                   WHERE datname = current_database())",
@@ -3684,6 +3686,7 @@ getSubscriptions(Archive *fout)
 	i_subname = PQfnumber(res, "subname");
 	i_rolname = PQfnumber(res, "rolname");
 	i_subenabled = PQfnumber(res, "subenabled");
+	i_subsynccommit = PQfnumber(res, "subsynccommit");
 	i_subconninfo = PQfnumber(res, "subconninfo");
 	i_subslotname = PQfnumber(res, "subslotname");
 	i_subpublications = PQfnumber(res, "subpublications");
@@ -3701,6 +3704,8 @@ getSubscriptions(Archive *fout)
 		subinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
 		subinfo[i].subenabled =
 			(strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
+		subinfo[i].subsynccommit =
+			(strcmp(PQgetvalue(res, i, i_subsynccommit), "t") == 0);
 		subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
 		subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
 		subinfo[i].subpublications =
@@ -3770,6 +3775,11 @@ dumpSubscription(Archive *fout, SubscriptionInfo *subinfo)
 	else
 		appendPQExpBufferStr(query, "DISABLED");
 
+	if (subinfo->subsynccommit)
+		appendPQExpBufferStr(query, ", SYNCHRONOUS_COMMIT = true");
+	else
+		appendPQExpBufferStr(query, ", SYNCHRONOUS_COMMIT = false");
+
 	appendPQExpBufferStr(query, ", SLOT NAME = ");
 	appendStringLiteralAH(query, subinfo->subslotname, fout);
 
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index a466527..5934eb0 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -604,6 +604,7 @@ typedef struct _SubscriptionInfo
 	DumpableObject dobj;
 	char	   *rolname;
 	bool		subenabled;
+	bool		subsynccommit;
 	char	   *subconninfo;
 	char	   *subslotname;
 	char	   *subpublications;
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index 3667374..4423982 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -4226,7 +4226,7 @@ qr/CREATE TRANSFORM FOR integer LANGUAGE sql \(FROM SQL WITH FUNCTION pg_catalog
 						 CONNECTION \'dbname=doesnotexist\' PUBLICATION pub1
 						 WITH (DISABLED, NOCONNECT);',
 		regexp       => qr/^
-			\QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (DISABLED, SLOT NAME = 'sub1');\E
+			\QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (DISABLED, SYNCHRONOUS_COMMIT = false, SLOT NAME = 'sub1');\E
 			/xm,
 		like => {
 			binary_upgrade           => 1,
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 8c58312..d3c51f4 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -5120,7 +5120,8 @@ describeSubscriptions(const char *pattern, bool verbose)
 	PQExpBufferData buf;
 	PGresult   *res;
 	printQueryOpt myopt = pset.popt;
-	static const bool translate_columns[] = {false, false, false, false, false};
+	static const bool translate_columns[] = {false, false, false, false,
+		false, false};
 
 	if (pset.sversion < 100000)
 	{
@@ -5146,7 +5147,9 @@ describeSubscriptions(const char *pattern, bool verbose)
 	if (verbose)
 	{
 		appendPQExpBuffer(&buf,
+						  ",  subsynccommit AS \"%s\"\n"
 						  ",  subconninfo AS \"%s\"\n",
+						  gettext_noop("Synchronous Commit"),
 						  gettext_noop("Conninfo"));
 	}
 
diff --git a/src/include/catalog/pg_subscription.h b/src/include/catalog/pg_subscription.h
index 0811880..62845e9 100644
--- a/src/include/catalog/pg_subscription.h
+++ b/src/include/catalog/pg_subscription.h
@@ -39,6 +39,7 @@ CATALOG(pg_subscription,6100) BKI_SHARED_RELATION BKI_ROWTYPE_OID(6101) BKI_SCHE
 
 	bool		subenabled;		/* True if the subscription is enabled
 								 * (the worker should be running) */
+	bool		subsynccommit;	/* Should apply use synchronous commit? */
 
 #ifdef CATALOG_VARLEN			/* variable-length fields start here */
 	text		subconninfo;	/* Connection string to the publisher */
@@ -54,14 +55,15 @@ typedef FormData_pg_subscription *Form_pg_subscription;
  *		compiler constants for pg_subscription
  * ----------------
  */
-#define Natts_pg_subscription					7
+#define Natts_pg_subscription					8
 #define Anum_pg_subscription_subdbid			1
 #define Anum_pg_subscription_subname			2
 #define Anum_pg_subscription_subowner			3
 #define Anum_pg_subscription_subenabled			4
-#define Anum_pg_subscription_subconninfo		5
-#define Anum_pg_subscription_subslotname		6
-#define Anum_pg_subscription_subpublications	7
+#define Anum_pg_subscription_subsynccommit		5
+#define Anum_pg_subscription_subconninfo		6
+#define Anum_pg_subscription_subslotname		7
+#define Anum_pg_subscription_subpublications	8
 
 
 typedef struct Subscription
@@ -71,6 +73,7 @@ typedef struct Subscription
 	char   *name;			/* Name of the subscription */
 	Oid		owner;			/* Oid of the subscription owner */
 	bool	enabled;		/* Indicates if the subscription is enabled */
+	bool	synccommit;		/* Indicates if apply should use synchronous commit */
 	char   *conninfo;		/* Connection string to the publisher */
 	char   *slotname;		/* Name of the replication slot */
 	List   *publications;	/* List of publication names to subscribe to */
diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out
index 0912bef..44229ec 100644
--- a/src/test/regress/expected/subscription.out
+++ b/src/test/regress/expected/subscription.out
@@ -25,19 +25,19 @@ ERROR:  invalid connection string syntax: missing "=" after "testconn" in connec
 CREATE SUBSCRIPTION testsub CONNECTION 'dbname=doesnotexist' PUBLICATION testpub WITH (NOCONNECT);
 WARNING:  tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
 \dRs+
-                               List of subscriptions
-  Name   |           Owner           | Enabled | Publication |      Conninfo       
----------+---------------------------+---------+-------------+---------------------
- testsub | regress_subscription_user | f       | {testpub}   | dbname=doesnotexist
+                                         List of subscriptions
+  Name   |           Owner           | Enabled | Publication | Synchronous Commit |      Conninfo       
+---------+---------------------------+---------+-------------+--------------------+---------------------
+ testsub | regress_subscription_user | f       | {testpub}   | f                  | dbname=doesnotexist
 (1 row)
 
 ALTER SUBSCRIPTION testsub SET PUBLICATION testpub2, testpub3 NOREFRESH;
 ALTER SUBSCRIPTION testsub CONNECTION 'dbname=doesnotexist2';
 \dRs+
-                                   List of subscriptions
-  Name   |           Owner           | Enabled |     Publication     |       Conninfo       
----------+---------------------------+---------+---------------------+----------------------
- testsub | regress_subscription_user | f       | {testpub2,testpub3} | dbname=doesnotexist2
+                                              List of subscriptions
+  Name   |           Owner           | Enabled |     Publication     | Synchronous Commit |       Conninfo       
+---------+---------------------------+---------+---------------------+--------------------+----------------------
+ testsub | regress_subscription_user | f       | {testpub2,testpub3} | f                  | dbname=doesnotexist2
 (1 row)
 
 BEGIN;
@@ -64,11 +64,12 @@ ALTER SUBSCRIPTION testsub RENAME TO testsub_dummy;
 ERROR:  must be owner of subscription testsub
 RESET ROLE;
 ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
-\dRs
-                          List of subscriptions
-    Name     |           Owner           | Enabled |     Publication     
--------------+---------------------------+---------+---------------------
- testsub_foo | regress_subscription_user | f       | {testpub2,testpub3}
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = true);
+\dRs+
+                                                List of subscriptions
+    Name     |           Owner           | Enabled |     Publication     | Synchronous Commit |       Conninfo       
+-------------+---------------------------+---------+---------------------+--------------------+----------------------
+ testsub_foo | regress_subscription_user | f       | {testpub2,testpub3} | t                  | dbname=doesnotexist2
 (1 row)
 
 -- rename back to keep the rest simple
diff --git a/src/test/regress/sql/subscription.sql b/src/test/regress/sql/subscription.sql
index c1199ee..84f6f36 100644
--- a/src/test/regress/sql/subscription.sql
+++ b/src/test/regress/sql/subscription.sql
@@ -45,8 +45,9 @@ ALTER SUBSCRIPTION testsub RENAME TO testsub_dummy;
 RESET ROLE;
 
 ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = true);
 
-\dRs
+\dRs+
 
 -- rename back to keep the rest simple
 ALTER SUBSCRIPTION testsub_foo RENAME TO testsub;
-- 
2.7.4


--------------4DC3589FBD93B4ED6512EA49
Content-Type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0


-- 
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

--------------4DC3589FBD93B4ED6512EA49--




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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





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

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

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

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

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


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread

* [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT()
@ 2023-01-21 00:09  Andres Freund <[email protected]>
  0 siblings, 0 replies; 270+ messages in thread

From: Andres Freund @ 2023-01-21 00:09 UTC (permalink / raw)

This just updates the places that "Zero initialize instr_time uses causing
compiler warnings" changed, to see whether this is a nicer approach.
---
 src/include/portability/instr_time.h | 24 ++++++++++++++------
 src/backend/access/transam/xlog.c    | 11 +++-------
 src/backend/storage/buffer/bufmgr.c  | 16 +++++---------
 src/backend/storage/file/buffile.c   | 16 +++++---------
 src/backend/storage/ipc/latch.c      |  8 +++----
 src/bin/psql/common.c                | 33 +++++++++++++---------------
 6 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index af2ab6ec887..2d1ff4f7f82 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -17,6 +17,10 @@
  *
  * INSTR_TIME_IS_LT(x, y)			x < y
  *
+ * INSTR_TIME_ZERO()				an instr_time set to 0
+ *
+ * INSTR_TIME_CURRENT()				an instr_time set to current time
+ *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
  * INSTR_TIME_SET_CURRENT(t)		set t to current time
@@ -110,7 +114,7 @@ typedef struct instr_time
 #define PG_INSTR_CLOCK	CLOCK_REALTIME
 #endif
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_clock_gettime_ns(void)
 {
@@ -123,8 +127,8 @@ pg_clock_gettime_ns(void)
 	return now;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_clock_gettime_ns())
+#define INSTR_TIME_CURRENT(t) \
+	pg_clock_gettime_ns()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = NS_PER_S * (s))
@@ -138,7 +142,7 @@ pg_clock_gettime_ns(void)
 
 /* Use QueryPerformanceCounter() */
 
-/* helper for INSTR_TIME_SET_CURRENT */
+/* helper for INSTR_TIME_CURRENT */
 static inline instr_time
 pg_query_performance_counter(void)
 {
@@ -160,8 +164,8 @@ GetTimerFrequency(void)
 	return (double) f.QuadPart;
 }
 
-#define INSTR_TIME_SET_CURRENT(t) \
-	((t) = pg_query_performance_counter())
+#define INSTR_TIME_CURRENT(t) \
+	pg_query_performance_counter()
 
 #define INSTR_TIME_SET_SECONDS(t, s) \
 	((t).ticks = s * GetTimerFrequency())
@@ -181,7 +185,13 @@ GetTimerFrequency(void)
 #define INSTR_TIME_IS_LT(x, y)	((x).ticks < (y).ticks)
 
 
-#define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
+#define INSTR_TIME_ZERO(t)	(instr_time){0}
+
+#define INSTR_TIME_SET_CURRENT(t) \
+	(t) = INSTR_TIME_CURRENT()
+
+#define INSTR_TIME_SET_ZERO(t) \
+	((t) = INSTR_TIME_ZERO())
 
 #define INSTR_TIME_SET_CURRENT_LAZY(t) \
 	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fb4c860bdea..f563800c8ab 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2178,7 +2178,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nbytes;
 			Size		nleft;
 			int			written;
-			instr_time	start;
+			instr_time	start = INSTR_TIME_ZERO();
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2191,8 +2191,6 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				/* Measure I/O timing to write WAL data */
 				if (track_wal_io_timing)
 					INSTR_TIME_SET_CURRENT(start);
-				else
-					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
 				written = pg_pwrite(openLogFile, from, nleft, startoffset);
@@ -2204,9 +2202,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 				 */
 				if (track_wal_io_timing)
 				{
-					instr_time	duration;
+					instr_time	duration = INSTR_TIME_CURRENT();
 
-					INSTR_TIME_SET_CURRENT(duration);
 					INSTR_TIME_SUBTRACT(duration, start);
 					PendingWalStats.wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
 				}
@@ -8137,7 +8134,7 @@ void
 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 {
 	char	   *msg = NULL;
-	instr_time	start;
+	instr_time	start = INSTR_TIME_ZERO();
 
 	Assert(tli != 0);
 
@@ -8153,8 +8150,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 	/* Measure I/O timing to sync the WAL file */
 	if (track_wal_io_timing)
 		INSTR_TIME_SET_CURRENT(start);
-	else
-		INSTR_TIME_SET_ZERO(start);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
 	switch (sync_method)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 800a4248c95..d8baf80e650 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1012,19 +1012,17 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 			MemSet((char *) bufBlock, 0, BLCKSZ);
 		else
 		{
-			instr_time	io_start,
-						io_time;
+			instr_time	io_start = INSTR_TIME_ZERO();
 
 			if (track_io_timing)
 				INSTR_TIME_SET_CURRENT(io_start);
-			else
-				INSTR_TIME_SET_ZERO(io_start);
 
 			smgrread(smgr, forkNum, blockNum, (char *) bufBlock);
 
 			if (track_io_timing)
 			{
-				INSTR_TIME_SET_CURRENT(io_time);
+				instr_time	io_time = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(io_time, io_start);
 				pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
 				INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time);
@@ -2826,8 +2824,7 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
-	instr_time	io_start,
-				io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 	Block		bufBlock;
 	char	   *bufToWrite;
 	uint32		buf_state;
@@ -2904,8 +2901,6 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * bufToWrite is either the shared buffer or a copy, as appropriate.
@@ -2918,7 +2913,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
 		INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 0a51624df3b..6f813279690 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -429,8 +429,7 @@ static void
 BufFileLoadBuffer(BufFile *file)
 {
 	File		thisfile;
-	instr_time	io_start;
-	instr_time	io_time;
+	instr_time	io_start = INSTR_TIME_ZERO();
 
 	/*
 	 * Advance to next component file if necessary and possible.
@@ -446,8 +445,6 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 		INSTR_TIME_SET_CURRENT(io_start);
-	else
-		INSTR_TIME_SET_ZERO(io_start);
 
 	/*
 	 * Read whatever we can get, up to a full bufferload.
@@ -468,7 +465,8 @@ BufFileLoadBuffer(BufFile *file)
 
 	if (track_io_timing)
 	{
-		INSTR_TIME_SET_CURRENT(io_time);
+		instr_time	io_time = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(io_time, io_start);
 		INSTR_TIME_ADD(pgBufferUsage.temp_blk_read_time, io_time);
 	}
@@ -500,8 +498,7 @@ BufFileDumpBuffer(BufFile *file)
 	while (wpos < file->nbytes)
 	{
 		off_t		availbytes;
-		instr_time	io_start;
-		instr_time	io_time;
+		instr_time	io_start = INSTR_TIME_ZERO();
 
 		/*
 		 * Advance to next component file if necessary and possible.
@@ -527,8 +524,6 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 			INSTR_TIME_SET_CURRENT(io_start);
-		else
-			INSTR_TIME_SET_ZERO(io_start);
 
 		bytestowrite = FileWrite(thisfile,
 								 file->buffer.data + wpos,
@@ -543,7 +538,8 @@ BufFileDumpBuffer(BufFile *file)
 
 		if (track_io_timing)
 		{
-			INSTR_TIME_SET_CURRENT(io_time);
+			instr_time	io_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(io_time, io_start);
 			INSTR_TIME_ADD(pgBufferUsage.temp_blk_write_time, io_time);
 		}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index f4123e7de7e..8092ff4a984 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1385,8 +1385,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 				 uint32 wait_event_info)
 {
 	int			returned_events = 0;
-	instr_time	start_time;
-	instr_time	cur_time;
+	instr_time	start_time = INSTR_TIME_ZERO();
 	long		cur_timeout = -1;
 
 	Assert(nevents > 0);
@@ -1401,8 +1400,6 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		Assert(timeout >= 0 && timeout <= INT_MAX);
 		cur_timeout = timeout;
 	}
-	else
-		INSTR_TIME_SET_ZERO(start_time);
 
 	pgstat_report_wait_start(wait_event_info);
 
@@ -1489,7 +1486,8 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
 		/* If we're not done, update cur_timeout for next iteration */
 		if (returned_events == 0 && timeout >= 0)
 		{
-			INSTR_TIME_SET_CURRENT(cur_time);
+			instr_time	cur_time = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
 			if (cur_timeout <= 0)
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index f907f5d4e8d..5badb029e83 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1269,15 +1269,12 @@ DescribeQuery(const char *query, double *elapsed_msec)
 	bool		timing = pset.timing;
 	PGresult   *result;
 	bool		OK;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 
 	*elapsed_msec = 0;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/*
 	 * To parse the query but not execute it, we prepare it, using the unnamed
@@ -1350,7 +1347,8 @@ DescribeQuery(const char *query, double *elapsed_msec)
 
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1400,16 +1398,13 @@ ExecQueryAndProcessResults(const char *query,
 {
 	bool		timing = pset.timing;
 	bool		success;
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	PGresult   *result;
 	FILE	   *gfile_fout = NULL;
 	bool		gfile_is_pipe = false;
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	if (pset.bind_flag)
 		success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0);
@@ -1490,7 +1485,8 @@ ExecQueryAndProcessResults(const char *query,
 			 */
 			if (timing)
 			{
-				INSTR_TIME_SET_CURRENT(after);
+				instr_time	after = INSTR_TIME_CURRENT();
+
 				INSTR_TIME_SUBTRACT(after, before);
 				*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 			}
@@ -1595,7 +1591,8 @@ ExecQueryAndProcessResults(const char *query,
 		 */
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1693,8 +1690,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 	int			ntuples;
 	int			fetch_count;
 	char		fetch_cmd[64];
-	instr_time	before,
-				after;
+	instr_time	before = INSTR_TIME_ZERO();
 	int			flush_error;
 
 	*elapsed_msec = 0;
@@ -1706,8 +1702,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 		INSTR_TIME_SET_CURRENT(before);
-	else
-		INSTR_TIME_SET_ZERO(before);
 
 	/* if we're not in a transaction, start one */
 	if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
@@ -1738,7 +1732,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
@@ -1786,7 +1781,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
 
 		if (timing)
 		{
-			INSTR_TIME_SET_CURRENT(after);
+			instr_time	after = INSTR_TIME_CURRENT();
+
 			INSTR_TIME_SUBTRACT(after, before);
 			*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 		}
@@ -1926,7 +1922,8 @@ cleanup:
 
 	if (timing)
 	{
-		INSTR_TIME_SET_CURRENT(after);
+		instr_time	after = INSTR_TIME_CURRENT();
+
 		INSTR_TIME_SUBTRACT(after, before);
 		*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
 	}
-- 
2.38.0


--vt42kbp2j7rjcapp--





^ permalink  raw  reply  [nested|flat] 270+ messages in thread


end of thread, other threads:[~2023-01-21 00:09 UTC | newest]

Thread overview: 270+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-03-06 12:07 [PATCH] Add option to modify sync commit per subscription Petr Jelinek <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-01-21 00:09 [PATCH v8 5/5] wip: instr_time: Add and use INSTR_TIME_ZERO(), INSTR_TIME_CURRENT() Andres Freund <[email protected]>
2023-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